// _compoments/luckDrawResultFrame/luckDrawResultFrame.js
let type = function (o) {
        let s = Object.prototype.toString.call(o)
        return s.match(/\[object (.*?)\]/)[1].toLowerCase()
};
['Null',
        'Undefined',
        'Object',
        'Array',
        'String',
        'Number',
        'Boolean',
        'Function',
        'RegExp'
].forEach(function (t) {
        type['is' + t] = function (o) {
                return type(o) === t.toLowerCase()
        }
})
import Address from "../../utils/address"; // pages/game_again/game_again.js                        
var mfApi = require('../../api/api.js');

const app = getApp();
Component({
        /**
         * 组件的属性列表
         */
        properties: {
                luckData: {
                        type: Object,
                        value: {},
                }
        },

        /**
         * 组件的初始数据
         */
        data: {
                baseUrl: app.globalData.baseUrl,
                provinceList: [],
                cityList: ['请先选择省'],
                areaList: ['请先选择市'],
                provinceIndex: null,
                cityIndex: null,
                areaIndex: null,

                // 输入的详细地址
                inputUserName: '',
                inputUserPhone: '',
                inputDetailAddress: '',
                isInputState: false,
        },

        attached() {
                let provinceList = this.getProvinceList()
                this.setData({
                        provinceList
                })
        },

        /**
         * 组件的方法列表
         */
        methods: {
                onNameInputChange(e) {
                        this.setData({
                                inputUserName: e.detail.value
                        })
                },

                onPhoneInputChange(e) {
                        this.setData({
                                inputUserPhone: e.detail.value
                        })
                },

                // 输入详细地址
                onDetailAddressInput(e) {
                        this.setData({
                                inputDetailAddress: e.detail.value
                        })
                },

                /**
                 * 关闭按钮点击
                 */
                onCloseIconClick() {
                        this.triggerEvent("onCloseClick");
                },

                onBtnClick() {
                        if (this.data.luckData.userAwardStatus == "0" ||
                                this.data.luckData.userAwardStatus == "1" ||
                                this.data.luckData.userAwardStatus == "-1") {
                                this.triggerEvent("onCloseClick");
                        } else if (this.data.luckData.userAwardStatus == "2") {
                                if (!this.data.isInputState) {
                                        this.setData({
                                                isInputState: true
                                        });
                                }

                        }

                        // if (!this.data.isLuckGet)
                        //         this.triggerEvent("onCloseClick");
                        // else if (this.data.isLuckGet)
                        //         this.setData({
                        //                 isInputState: true
                        //         });
                },

                async onBtnCommitClick() {
                        // this.triggerEvent("onCloseClick");
                        // 检查数据
                        let {
                                inputUserName,
                                inputUserPhone,
                                provinceIndex,
                                cityIndex,
                                areaIndex,
                                inputDetailAddress
                        } = this.data;
                        let str = "";
                        const phoneRegex = /^1[3456789]\d{9}$/; //使用此正则即可
                        if (!inputUserName || inputUserName.length == 0) {
                                str = "请输入姓名";
                        } else if (!phoneRegex.test(inputUserPhone)) {
                                str = "请输入正确的手机号";
                        } else if (!provinceIndex || !cityIndex || !areaIndex) {
                                str = "请选择完整区域";
                        } else if (!inputDetailAddress || inputDetailAddress.length == 0) {
                                str = "请输入详细地址";
                        }

                        if (str.length > 0) {
                                wx.showToast({
                                        title: str,
                                        icon: "none"
                                })
                                return;
                        }

                        wx.showLoading({
                                title: "加载中",
                        });

                        let temp = {
                                "address": inputDetailAddress,
                                "area": `${this.data.provinceList[provinceIndex]}, ${this.data.cityList[cityIndex]}, ${this.data.areaList[areaIndex]}`,
                                "mobile": inputUserPhone,
                                "name": inputUserName
                        };

                        let res = await mfApi.http(mfApi.saveAddress(app.globalData.openId, this.data.luckData.userAwardId), "POST", temp)

                        wx.hideLoading();
                        if (res.code != 200) {
                                wx.showToast({
                                        title: res.message,
                                        icon: "none"
                                })
                                return;
                        }

                        wx.showToast({
                                title: '奖品信息提交成功',
                                icon: "none"
                        })

                        this.onCloseIconClick();
                },

                /** 获取省份列表
                 * return provinceList: 返回省份列表
                 */
                getProvinceList() {
                        let provinceList = []
                        for (let i = 0; i < Address.length; i++) {
                                provinceList.push(Address[i].name)
                                console.log("====provinceList===", provinceList)
                        }
                        return provinceList
                },


                /** 获取城市列表
                 * provinceIndex: 省份索引
                 * return cityList: 返回城市列表
                 */
                getCityList(provinceIndex, provinceName = '') {
                        let cityList = []

                        if (type.isNull(provinceIndex) || type.isUndefined(provinceIndex)) return

                        let citys = Address[provinceIndex].cityList
                        for (let i = 0; i < citys.length; i++) {
                                cityList.push(citys[i].name)
                        }
                        return cityList
                },

                /** 获取区县列表
                 * provinceIndex: 省份索引
                 * cityIndex: 城市索引
                 * return areaList: 返回区县列表
                 */
                getAreaList(provinceIndex, cityIndex, cityName = '', cityList = []) {
                        if (type.isNull(provinceIndex) || type.isUndefined(provinceIndex) || type.isNull(cityIndex) || type.isUndefined(cityIndex)) {
                                return
                        }

                        let areaList = Address[provinceIndex].cityList[cityIndex].areaList
                        return areaList
                },

                // 选择省市区
                chooeseAddress(e) {
                        const {
                                value
                        } = e.detail
                        const {
                                mark
                        } = e.currentTarget.dataset

                        this.setData({
                                provinceName: '',
                                cityName: '',
                                countyName: '',
                                detailAddress: ''
                        })

                        // 选择省
                        if (mark === 'province') {
                                this.setData({
                                        provinceIndex: value,
                                        cityIndex: null,
                                        areaIndex: null
                                })
                                let cityList = this.getCityList(value)
                                this.setData({
                                        cityList,
                                        areaList: ['请先选择市']
                                })
                        }

                        // 选择市
                        if (mark === 'city') {
                                let {
                                        provinceIndex
                                } = this.data

                                if (type.isNull(provinceIndex) || type.isUndefined(provinceIndex)) return

                                this.setData({
                                        cityIndex: value,
                                        areaIndex: null
                                })

                                let areaList = this.getAreaList(this.data.provinceIndex, this.data.cityIndex)
                                this.setData({
                                        areaList
                                })
                        }

                        // 选择区
                        if (mark === 'area') {
                                let {
                                        provinceIndex,
                                        cityIndex
                                } = this.data
                                if (type.isNull(provinceIndex) || type.isUndefined(provinceIndex) || type.isNull(cityIndex) || type.isUndefined(cityIndex)) {
                                        return
                                }
                                this.setData({
                                        areaIndex: value
                                })
                        }
                },
        },


})